package com.tim.app.ui.activity.person_center;

import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

import com.application.library.net.StringResponseCallback;
import com.application.library.widget.EmptyLayout;
import com.application.library.widget.loadmore.LoadMoreContainer;
import com.application.library.widget.loadmore.LoadMoreHandler;
import com.application.library.widget.loadmore.LoadMoreRecycleViewContainer;
import com.application.library.widget.recycle.HorizontalDividerItemDecoration;
import com.application.library.widget.recycle.WrapRecyclerView;
import com.google.gson.Gson;
import com.tim.app.R;
import com.tim.app.server.api.API_User;
import com.tim.app.server.entry.WithdrawalRecoderBean;
import com.tim.app.server.logic.UserManager;
import com.tim.app.server.result.IncomeDetailBean;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.server.result.WithdrawalBean;
import com.tim.app.ui.activity.BaseActivity;
import com.tim.app.ui.adapter.personal.WithdrawalRecoderAdapter;
import com.tim.app.ui.fragment.person.IncomeJasmineFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wdy on 2017/8/23.
 */

public class WithdrawalRecordActivity extends BaseActivity {
    private LoadMoreRecycleViewContainer load_more;
    private WrapRecyclerView wrvPersonial;
    private EmptyLayout emptyLayout;
    private ImageButton ib_back;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_withdrawarecoder;
    }

    @Override
    public void onClick(View v) {
        if (v == ib_back) {
            finish();
        }
    }

    @Override
    public void initView() {
        ib_back = (ImageButton) findViewById(R.id.ib_back);
        ib_back.setOnClickListener(this);
        load_more = (LoadMoreRecycleViewContainer) findViewById(R.id.load_more);
        wrvPersonial = (WrapRecyclerView) findViewById(R.id.wrvPersonial);
        wrvPersonial.setOverScrollMode(View.OVER_SCROLL_NEVER);
        emptyLayout = new EmptyLayout(this, load_more);
        //  emptyLayout.showLoading();
        emptyLayout.setEmptyDrawable(R.drawable.ic_empty_follow);
        emptyLayout.setEmptyText("你还没有提现记录", "");
        emptyLayout.setErrorButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentPage = 1;
                loadDte(currentPage);
            }
        });
    }

    private int currentPage = 1;
    private WithdrawalRecoderAdapter myInvatePersonAdapter;
    private LoadMoreHandler loadMoreHandler = new LoadMoreHandler() {
        @Override
        public void onLoadMore(LoadMoreContainer loadMoreContainer) {
            loadDte(currentPage + 1);
        }
    };

    @Override
    public void initData() {
        load_more.useDefaultFooter(View.GONE);
        load_more.setAutoLoadMore(true);
        load_more.setLoadMoreHandler(loadMoreHandler);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        wrvPersonial.setLayoutManager(layoutManager);
        wrvPersonial.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).color(getResources().getColor(R.color.transparent)).size((int) getResources().getDimension(R.dimen.dimen_5)).build());
        emptyLayout.showLoading();
        myInvatePersonAdapter = new WithdrawalRecoderAdapter(this, result);
        wrvPersonial.setAdapter(myInvatePersonAdapter);
        loadDte(currentPage);
    }

    private List<WithdrawalBean.ResBean.ArraylistBean.ResultBean> result = new ArrayList<>();

    private void loadDte(int currentPage) {
        API_User.ins().depositMoneyRecoder(TAG, UserManager.ins().getUserId(), currentPage, 10, new StringResponseCallback() {
            @Override
            public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                Log.e("tag", "resultValue====" + resultValue);
                if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                    WithdrawalBean withdrawalBean = new Gson().fromJson(resultValue, WithdrawalBean.class);
                    if (withdrawalBean.getRes() == null || withdrawalBean.getRes().getArraylist() == null || withdrawalBean.getRes().getArraylist().getResult() == null) {
                        emptyLayout.showEmpty();
                        return false;
                    } else {
                        result.addAll(withdrawalBean.getRes().getArraylist().getResult());
                    }
                    if (result.size() == 0) {
                        emptyLayout.showEmpty();
                    } else {
                        emptyLayout.showContent();
                    }
                    WithdrawalRecordActivity.this.currentPage = withdrawalBean.getRes().getArraylist().getCurrentPageNo();
                    if (withdrawalBean.getRes().getArraylist().getCurrentPageNo() == withdrawalBean.getRes().getArraylist().getTotalPageCount()) {
                        load_more.loadMoreFinish(false, false);
                    } else {
                        load_more.loadMoreFinish(false, true);
                    }
                    myInvatePersonAdapter.notifyDataSetChanged();
                } else {
                    emptyLayout.showError();
                }
                return false;
            }
        });
    }
}
